package org.vineatoum.model;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import org.vineatoum.business.Exploitation;
import org.vineatoum.business.configuration.Configuration;
import org.vineatoum.model.abstraction.IExploitationManager;

@XmlRootElement(name="vineatoum")
public class ExploitationManager implements IExploitationManager {
	@XmlElement
	Exploitation exploitation;
	@XmlElement
	Configuration configuration;

	public ExploitationManager() {
	
	}
	
	@Override
	public Exploitation getExploitation() {
		return exploitation;
	}

	@Override
	public void updateExploitation(Exploitation exploitationOld) {
		exploitation.updateExploitation(exploitationOld);
		// TODO Persist
		try {
			JAXBContext jc = JAXBContext.newInstance(ExploitationManager.class);
			Marshaller m = jc.createMarshaller();
			m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
	        m.marshal(this, new FileOutputStream(getClass().getResource("/exploitation.xml").getPath()));
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public Configuration getConfiguration() {
		return configuration;
	}

	@Override
	public void updateConfiguration(Configuration config) {
		configuration = config;
		// TODO Persist
		try {
			JAXBContext jc = JAXBContext.newInstance(ExploitationManager.class);
			Marshaller m = jc.createMarshaller();
			m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
			m.marshal(this, new FileOutputStream(getClass().getResource("/exploitation.xml").getPath()));
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
